package com.cy.ppj.web.comm;

import com.cy.ppj.model.Result;
import com.cy.ppj.model.domian.user.UserWechat;
import com.cy.ppj.model.dto.user.UserLoginDTO;
import com.cy.ppj.model.enums.wechat.WechatSopce;
import com.cy.ppj.service.wechat.WechatService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @description:
 * @author: Ocean
 * @create: 2018-07-14 14:57
 **/
@Slf4j
@RestController
@RequestMapping("/wechat")
@Api(tags = "前台-微信")
public class WechatController extends BaseController {

    @Autowired
    private WechatService wechatService;
    @Value("${app.home.page}")
    private String appHomePage;


    @ApiOperation("公众号验证token")
    @GetMapping("/verify/token")
    public String verifyToken(String echostr) {
        return echostr;
    }

    @ApiOperation("绑定授权回调方法")
    @GetMapping("/bind/callback")
    public ModelAndView bindCallback(String code, String state) {

        UserWechat userWechat = wechatService.callback(code, state);

        wechatService.bindUser(userWechat);

        return new ModelAndView("redirect:".concat(appHomePage));
    }

    @ApiOperation("解绑微信")
    @GetMapping("/unbind")
    public Result unbind() {

        Long userId = this.getUser().getUserId();

        wechatService.unbind(userId);

        return ok();
    }

    @ApiOperation("微信自动登录回调方法")
    @GetMapping("/login/callback")
    public ModelAndView baseCallback(String code) {
        Result result = wechatService.autoLogin(code);
        String url = "redirect:".concat(appHomePage);
        if (result.isOk()) {
            UserLoginDTO user = (UserLoginDTO) result.getValue();
            url = url.concat("?token=").concat(user.getToken()).concat("&valid=1");
        } else {
            url = url.concat("?valid=0");
        }
        return new ModelAndView(url);
    }

    @ApiOperation("获取微信自动登录回调地址")
    @GetMapping("/login/url")
    public Result loginUrl() {
        String url = wechatService.getAutoLoginUrl();
        return ok(null, url);
    }

    @ApiOperation("获取用户授权地址url")
    @GetMapping("/auth/url")
    public Result userBind(WechatSopce wechatSopce) {

        Long userId = this.getUser().getUserId();
        String url = wechatService.getAuthorizeUrl(userId, wechatSopce);
        return ok(null, url);
    }

    @ApiOperation("获取微信分享签名")
    @GetMapping("/share/signature")
    public Result getShareSignature(String url) {

        String token = wechatService.getAccessToken();
        String ticket = wechatService.getTicket(token);

        Map<String, String> map = wechatService.getSignature(token, ticket, url);

        return ok("获取TOKEN成功", map);
    }

}
